﻿using BXH.Movie.Bll;
using BXH.Movie.FrmUsers;
using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BXH.Movie.Forms
{
    public partial class UCOrderManager : UserControl
    {
        private OrderInfoService orderInfoService = new OrderInfoService();
        public UCOrderManager()
        {
            InitializeComponent();
            this.Load += UCOrderManager_Load;
        }

        private void UCOrderManager_Load(object sender, EventArgs e)
        {
            dgvOrder.DataSource = null;
            var orders = orderInfoService.GetByUserId(Program.LoginUser.Id);
            dgvOrder.DataSource = orders;
            dgvOrder.AutoGenerateColumns = false;
            DataGridViewLinkColumn dlink = new DataGridViewLinkColumn();
            dlink.HeaderText = "退订";//列的标题
            dlink.Text = "退订";
            dlink.UseColumnTextForLinkValue = true;//上面设置的dlink.Text文字在列中显示
            dgvOrder.Columns.Add(dlink);//将创建的列添加到UserdataGridView中
            dgvOrder.CellMouseClick += DgvOrder_CellMouseClick;
        }

        private void DgvOrder_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 6)
            {
                var row = dgvOrder.Rows[e.RowIndex];
                var id = Convert.ToInt32(row.Cells[0].Value);
                if (orderInfoService.Delete(id))
                {
                    MessageBoxEx.Show("取消订单成功");
                    dgvOrder.DataSource = null;
                    var orders = orderInfoService.GetByUserId(Program.LoginUser.Id);
                    dgvOrder.DataSource = orders;
                }
                else
                {
                    MessageBoxEx.Show("取消订单失败");
                }
            }
        }
    }
}
